package com.ruoyi.web.controller.performance;

import com.etoppaas.performance.domain.PerAppraisalInfo;
import com.etoppaas.performance.domain.PerAppraisalSpec;
import com.etoppaas.performance.domain.PerTemplate;
import com.etoppaas.performance.domain.PerTemplateSpecRelation;
import com.etoppaas.performance.service.IPerAppraisalInfoService;
import com.etoppaas.performance.service.IPerAppraisalSpecService;
import com.etoppaas.performance.service.IPerTemplateService;
import com.etoppaas.performance.vo.FormulateVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  信息操作处理
 * 
 * @author ruoyi
 * @date 2019-06-05
 */
@Controller
@RequestMapping("/performance/perAppraisalInfo")
public class PerAppraisalInfoController extends BaseController
{
    private String prefix = "performance/perAppraisalInfo";
	
	@Autowired
	private IPerAppraisalInfoService perAppraisalInfoService;

	@Autowired
	private IPerTemplateService perTemplateService;

	@Autowired
	private IPerAppraisalSpecService appraisalSpecService;
	
	@RequiresPermissions("performance:perAppraisalInfo:view")
	@GetMapping()
	public String perAppraisalInfo()
	{
	    return prefix + "/perAppraisalInfo";
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("performance:perAppraisalInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(PerAppraisalInfo perAppraisalInfo)
	{
		startPage();
        List<PerAppraisalInfo> list = perAppraisalInfoService.selectPerAppraisalInfoList(perAppraisalInfo);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出列表
	 */
	@RequiresPermissions("performance:perAppraisalInfo:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PerAppraisalInfo perAppraisalInfo)
    {
    	List<PerAppraisalInfo> list = perAppraisalInfoService.selectPerAppraisalInfoList(perAppraisalInfo);
        ExcelUtil<PerAppraisalInfo> util = new ExcelUtil<PerAppraisalInfo>(PerAppraisalInfo.class);
        return util.exportExcel(list, "perAppraisalInfo");
    }
	
	/**
	 * 新增
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}

	/**
	 * 制定考核任务
	 * @return
	 */
	@RequiresPermissions("performance:perAppraisalInfo:formulate")
	@GetMapping("/formulate")
	public String formulate()
	{
		return prefix + "/formulate";
	}


	/**
	 * 新增保存
	 */
	@RequiresPermissions("performance:perAppraisalInfo:add")
	@Log(title = "", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(PerAppraisalInfo perAppraisalInfo)
	{		
		return toAjax(perAppraisalInfoService.insertPerAppraisalInfo(perAppraisalInfo));
	}

	/**
	 * 修改
	 */
	@GetMapping("/edit/{pkid}")
	public String edit(@PathVariable("pkid") Long pkid, ModelMap mmap)
	{
		PerAppraisalInfo perAppraisalInfo = perAppraisalInfoService.selectPerAppraisalInfoById(pkid);
		mmap.put("perAppraisalInfo", perAppraisalInfo);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存
	 */
	@RequiresPermissions("performance:perAppraisalInfo:edit")
	@Log(title = "", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(PerAppraisalInfo perAppraisalInfo)
	{		
		return toAjax(perAppraisalInfoService.updatePerAppraisalInfo(perAppraisalInfo));
	}
	
	/**
	 * 删除
	 */
	@RequiresPermissions("performance:perAppraisalInfo:remove")
	@Log(title = "", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(perAppraisalInfoService.deletePerAppraisalInfoByIds(ids));
	}


	/**
	 * 加载部门员工列表树
	 */
	@GetMapping("/userTreeData")
	@ResponseBody
	public List<Ztree> userZtree()
	{
		List<Ztree> ztrees = perAppraisalInfoService.initUserZtree();
		return ztrees;
	}

	/**
	 * 保存绩效计划
	 * @param vo
	 * @return
	 */
	@PostMapping("/saveFormulate")
	@ResponseBody
	public AjaxResult saveFormulate(FormulateVo vo)
	{
		return toAjax(perAppraisalInfoService.saveFormulate(vo));
	}

	/**
	 * 开启员工自评
	 * @param ids
	 * @return
	 */
	@RequiresPermissions("performance:perAppraisalInfo:enabledEva")
	@Log(title = "", businessType = BusinessType.INSERT)
	@PostMapping("/enabledEva")
	@ResponseBody
	public AjaxResult enabledEva(@RequestParam(value = "ids")List<Long> ids)
	{
		return toAjax(perAppraisalInfoService.enabledEva(ids));
	}


    /**
     * 绩效自评
     * @param pkid
     * @param mmap
     * @return
     */
    @GetMapping("/evaluation/{pkid}")
    public String evaluation(@PathVariable("pkid") Long pkid,String status, ModelMap mmap)
    {
        PerAppraisalInfo perAppraisalInfo = perAppraisalInfoService.selectPerAppraisalInfoById(pkid);
        mmap.put("appraisalInfo", perAppraisalInfo);
		PerAppraisalSpec spec = new PerAppraisalSpec();
		spec.setAppraisalId(pkid);
        List<PerAppraisalSpec> specList = appraisalSpecService.selectPerAppraisalSpecList(spec);
        mmap.put("specList",specList);
        mmap.put("status",status);
        return prefix + "/selfEvaluation";
    }

	/**
	 * 绩效自评
	 * @param pkid
	 * @param mmap
	 * @return
	 */
	@GetMapping("/viewDetail/{pkid}")
	public String viewDetail(@PathVariable("pkid") Long pkid,String status, ModelMap mmap)
	{
		PerAppraisalInfo perAppraisalInfo = perAppraisalInfoService.selectPerAppraisalInfoById(pkid);
		mmap.put("appraisalInfo", perAppraisalInfo);
		PerAppraisalSpec spec = new PerAppraisalSpec();
		spec.setAppraisalId(pkid);
		List<PerAppraisalSpec> specList = appraisalSpecService.selectPerAppraisalSpecList(spec);
		mmap.put("specList",specList);
		mmap.put("status",status);
		return prefix + "/selfEvaluation";
	}
	/**
	 * 自评保存
	 * @param info
	 * @return
	 */
	@Log(title = "保存自评", businessType = BusinessType.INSERT)
	@PostMapping("/saveSelfEvaluation")
	@ResponseBody
	public AjaxResult saveSelfEvaluation(PerAppraisalInfo info)
	{
		return  toAjax(perAppraisalInfoService.saveSelfEvaluation(info));
	}



	@RequiresPermissions("performance:perAppraisalInfo:staffEva")
	@GetMapping("/staffEva")
	public String staffEva()
	{
		return prefix + "/staffEva";
	}

	@GetMapping("/appraisalSpecConfirm/{templateId}")
	public String appraisalSpecConfirm(@PathVariable("templateId")Long templateId,ModelMap mmap)
	{
		PerTemplate perTemplate = perTemplateService.selectPerTemplateById(templateId);
		List<PerTemplateSpecRelation> relations= perTemplateService.getDistributeData(templateId);
		mmap.put("relations",relations);
		mmap.put("perTemplate", perTemplate);
		mmap.put("templateId",templateId);
		return prefix + "/appraisalSpecConfirm";
	}

}
